﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.Common;

public partial class Detail : System.Web.UI.Page
{
    Topic topic = new Topic();
    Reply reply = new Reply();
    int iTopicID;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["UserName"] == null)
        {
            Response.Write("<script language=javascript>alert('请先登录！')</script>");
            Response.Redirect("Login.aspx");
        }
        else
        {
            iTopicID = Convert.ToInt32(Request.QueryString["TopicID"]);
            DbDataReader dr = topic.GetTopicDetail(Convert.ToInt32(Request.QueryString["TopicID"]));
            if (dr.Read())
            {
                lblTopicTitle.Text = dr["TopicTitle"].ToString();
                lblUserName.Text = dr["UserName"].ToString();
                lblIP.Text = dr["UserIP"].ToString();
                lblCreateDate.Text = dr["CreateDate"].ToString();
                lblContent.Text = HttpUtility.HtmlEncode(dr["TopicContent"].ToString());
            }
            dr.Close();
            dr.Dispose();
            DataSet ds = reply.GetReplyByTopicID(Convert.ToInt32(Request.QueryString["TopicID"]));
            if (ds.Tables[0].Rows.Count > 0)
            {
                foreach (DataRow datarow in ds.Tables[0].Rows)
                {
                    lblReplyList.Text +="标题："+"【" + datarow["ReplyTitle"].ToString() + "】"
                        +"<br>"+"信息："+datarow["UserName"].ToString()
                        +"|"+datarow["ReplyDate"].ToString()
                        +"|"+datarow["UserIP"].ToString()
                        +"<br>"+"回复内容："+datarow["ReplyContent"].ToString()
                        +"<br>"+"<hr>";
                }
            }
        }
    }
    protected void btnGoBack_Click(object sender, EventArgs e)
    {
        Response.Redirect("TopicList.aspx");
    }
    protected void btnReply_Click(object sender, EventArgs e)
    {
        if (Session["UserName"].ToString() == "Guest")
        {
            Response.Write("<script language=javascript>alert('您无权回复！')</script>");
        }
        else
        {
            if (txtReplyTitle.Text.Trim() == "")
            {
                Response.Write("<script language=javascript>alert('回复主题不能为空！')</script>");
            }
            else if (txtReplyContent.Text.Trim() == "")
            {
                Response.Write("<script language=javascript>alert('回复内容不能为空！')</script>");
            }
            else
            {
                int iResult = reply.ReplyTopic(iTopicID, Session["UserName"].ToString(),
                    txtReplyTitle.Text.Trim(), txtReplyContent.Text, DateTime.Now,
                    Request.UserHostAddress);
                if (iResult > 0)
                {
                    txtReplyTitle.Text = "";
                    txtReplyContent.Text = "";
                    lblReplyList.Text = "";
                }
                else
                {
                    Response.Write("<script language=javascript>alert('回复失败！')</script>");
                }
                Page_Load(sender, e);
            }
        }
    }
}
